package org.example.hadoop.service.publishYearFrequency;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.example.hadoop.domain.PublisherCountBean;

import java.io.IOException;
import java.util.regex.Pattern;

/**
 * @author wwy
 * @date 2020-11-10 18:56
 */
public class PublishYearMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
    private static final IntWritable valueInfo = new IntWritable(1);
    private static Text keyInfo = new Text();
    /**
     * k1      v1
     *         https://book.douban.com/subject/4913064/ 活着 小说 余华 9.4 531574 20.00元 作家出版社 2012-8-1 132697
     *
     * k2      v2
     * 2012    1
     * @param key
     * @param value
     * @param context
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String line = value.toString();
        String[] valueArray = line.split(" ");
        String publishYear = valueArray[8];
        if(publishYear.length()<= 3){
            return;
        }
        String publishYear1 = publishYear.substring(0, 4);
        Pattern pattern = Pattern.compile("[0-9]*");
        if(pattern.matcher(publishYear1).matches()){
            keyInfo.set(publishYear1);
            context.write(keyInfo,valueInfo);
        }

    }
}
